/*
 * chinanet 接口总文件
 * by wzdxx1314@163.com
 * 2021-6-1
 * */

const axios = require('axios');
const qs = require('qs')
import {
    Message
} from 'element-ui'
import router from "../router";

const instance = axios.create({
    baseURL: '/dev-api',
    // baseURL: '/apicz/',
    timeout: 50000,
});
let goLogin = false

/*
 *
 * 请求的预处理 by wzd
 * */
instance.interceptors.request.use((config) => {
    if (config.url === 'api/system/external/login') {
        config.headers['biyiCaptchaKey'] = window.biyiCaptchaKey;
        config.headers['biyiCaptcha'] = JSON.stringify({
            code: window.biyiCaptcha
        });
        config.headers['Content-Type'] = 'application/json;charset=UTF-8';
    }
    if(config.url.includes('/api/external/schedules/exportLoadAndUpload')){
        config.headers['Content-Type'] = 'application/vnd.ms-excel';
        config.responseType='blob'
        config.connection = 'keep-alive'
    }
    if(config.url.includes('/api/external/planTruckCar/exportLoadAndUpload')){
        config.responseType='arraybuffer'
        config.connection = 'keep-alive'
    }
    if(config.url.includes('/api/external/vehicle-detail/export')){
        config.responseType='arraybuffer'
        config.connection = 'keep-alive'
    }
    config.headers['Accept'] = '';
    config.headers['X-Requested-With'] = 'XMLHttpRequest';
    config.headers['Authorization'] = localStorage.getItem('token')
    return config;
}, (error) => {
    return Promise.reject(error);
});

/*
 *
 * 返回的预处理 by wzd
 * */
instance.interceptors.response.use(function (response) {
    // Do something with response data
    return response.data;
}, function (error) {
    return Promise.reject(error);
});
// let messageInstance = null; // 默认 Message 未打开
function fetch(ourl, params = {}, methods = 'post') {
    return new Promise((resolve, reject) => {
        let url = ourl;
        if (methods == 'get' && qs.stringify(params)) {
            url = ourl + '?' + qs.stringify(params);
        }
        instance[methods](url, params).then(res => {
            if (res.code !== undefined) {
                if (res.code == 200) {
                    resolve(res)
                } else if (res.code == 401) {
                    reject(res)
                    if(ourl == 'api/external/queryMineInfo' || ourl == '/api/external/bMessage/queryMessage'){
                        /* 首页用到的接口 */
                    }else{
                        router.push({name:'login'})
                    }

                }else{
                    Message({
                        type: 'error',
                        message: res.message,
                    });
                    reject(res)
                }
            } else {
                resolve(res)
            }

            // if (ourl == 'api/catpcha/digitalCaptcha' || ourl == 'api/system/login' || ourl == 'api/system/loginSSO') {
            //     resolve(res)
            // } else {
            //
            //     if (res.code == 200) {
            //         resolve(res)
            //     }else if(res.code == 501 || res.code == 401){
            //         if(goLogin == false){
            //             let localHref = encodeURIComponent(`${window.location.origin}/#/singleLogin`)
            //             let href = `http://iam.jxsgkjt.com/esc-sso/oauth2.0/authorize?client_id=038999a4bbf8894b4588&response_type=code&redirect_uri=${localHref}`
            //             window.open(href,'_self')
            //             goLogin = true
            //         }
            //         reject(res)
            //     } else {
            //         Message({
            //             type: 'error',
            //             message: res.message,
            //         });
            //         reject(res)
            //     }
            // }


        }).catch((err) => {
            // if (err.response?.data?.status === 401) {
            //     const resetMessage = (options) => {
            //         if (messageInstance) messageInstance.close() // 判断弹窗是否已存在,若存在则关闭
            //         messageInstance = Message(options)
            //     }
            //     // message类型
            //     const typeArr = ['success', 'error', 'warning', 'info']
            //     typeArr.forEach(type => {
            //         resetMessage[type] = options => {
            //             if (typeof options === 'string') options = {
            //                 message: options
            //             }
            //             options.type = type
            //             return resetMessage(options)
            //         }
            //     })
            //     resetMessage({
            //         showClose: true,
            //         message: '您的登录已过期',
            //         type: 'error'
            //     })
            //
            //
            // }
            reject(err)
        })
    })
}

export default {
    //运营人航线
    queryDataStandard(params){
        return fetch('api/external/schedules/serviceLines', params,)
    },
    getDicsByCriteria(params){
        return fetch('api/admin/getDicsByCriteria', params,)
    },
    getSelfApiKeys(params) {
        return fetch('api/ApiApplication/paginQuery', params,)
    },
    addApikey(params) {
        return fetch('api/ApiApplication/add', params,)
    },
    applyApiService(params) {
        return fetch('api/ApiApplication/applyApiService', params,)
    },
    outApiPaginQuery(params) {
        return fetch('api/ApiApplication/outPaginQuery', params,'get')
    },
    getApiDetial(params) {
        return fetch('api/ApiSystem/apiDetail', params,'get')
    },
    goodsName(params) {
		return fetch('/api/external/schedules/goodsName', params, 'get')
	},
    goodsNewName(params) {
		return fetch('/api/external/getGoodsType', params, 'get')
	},
    
    // customerByType(params){
    //     return fetch('/api/external/schedules/customerByType', params,)
    // },
    queryCntrnoByPlanNo(params) {
		return fetch('/api/external/planTruckCar/queryCntrnoByPlanNo', params, 'get')
	},
    term(params) {
		return fetch('/api/external/schedules/term', params, 'get')
	},
    // 车队计划
    queryCar(params) {
        return fetch('/api/external/planTruckCar/queryCar', params,'get')
    },
    queryGoodsType(params) {
        return fetch('/api/external/planTruckCar/queryGoodsType', params,'get')
    },
    queryWithCargo(params) {
        return fetch('/api/external/planTruckCar/queryWithCargo', params,'get')
    },
    queryWithCntr(params) {
        return fetch('/api/external/planTruckCar/queryWithCntr', params,'get')
    },

    planTruckCarSubmit(params) {
        return fetch('/api/external/planTruckCar/submit', params)
    },


    contentSelectAll(params) {
        return fetch('/api/external/All_Select', params)
    },

    detailGoods(params) {
        return fetch('/api/JxgwPlanGoods/detail', params, 'get')
    },

    queryMessage(params) {
        return fetch('/api/message/queryMessage', params, 'get')
    },
    uploadFile(params) {
        return fetch('/api/external/uploadFile', params,)
    },

    getDangerUnionno(params) {
        return fetch('/api/external/getDangerUnionno', params, 'get')
    },
    searchHome(params) {
        return fetch('/api/content/searchHome', params, )
    },
    tosGetContainer(params) {
        return fetch('/api/external/tosGetContainer', params, )
    },
    voyagesPage(params) {
        return fetch('/api/external/voyagesPage', params, 'get')
    },
    getCustomerRelations(params) {
        return fetch('/api/external/getCustomerRelations', params, 'get')
    },
    setRelation(params) {
        return fetch('/api/external/setRelation', params,)
    },
    
    QueryDataStandard(params) {
        return fetch('/api/external/schedules/serviceLines', params, 'get')
    },
    getContentHome(params) {
        return fetch('api/content/paginHome', params, )
    },

    //获取货类编码
    getGoodsType(params) {
        return fetch('/api/external/getGoodsType', params, 'get')
    },
    // 获取船舶编码
    getVessels(params) {
        return fetch('/api/external/getVessels', params, 'get')
    },
    planCancel(params) {
        return fetch('/api/external/planCancel', params,)
    },
    tosGetGoods(params) {
        return fetch('/api/external/tosGetGoods', params,)
    },
    login(params) {
        return fetch('api/system/external/login', params,)
    },
    getPublicKey(params) {
        return fetch('api/external/getPublicKey', params,'get')
    },
    updatePassword(params) {
        return fetch('api/external/updatePassword', params,)
    },
    getMessageCode(params) {
        return fetch('api/external/getMessageCode', params,'get')
    },
    //验证码
    getDigitalCaptcha(params) {
        return fetch('api/catpcha/digitalCaptcha', params, 'get')
    },
    //获取当前用户信息
    getUser(params) {
        return fetch('api/external/queryMineInfo', params, 'get')
    },
    getCustomerInfo(params) {
        return fetch('api/external/getCustomerInfo', params, 'get')
    },
    editCustomerInfo(params) {
        return fetch('api/external/editCustomerInfo', params, )
    },
    register(params) {
        return fetch('api/external/register', params,)
    },
    getCustomerTypeList(params) {
        return fetch('api/external/customerTypeList', params, 'get')
    },
    paginQuery(params) {
        return fetch('/api/JxgwPlanGoods/paginQuery', params,)
    },
    shipE(params, url) {
        return fetch('/api/external/' + url, params,)
    },
    /*卸船入库*/
    xAdd(params) {
        return fetch('/api/external/XCRK_G_Add', params,)
    },
    xEdit(params) {
        return fetch('/api/external/XCRK_G_Edit', params,)
    },
    /*卸船直提*/
    xcAdd(params) {
        return fetch('/api/external/XCZT_G_Add', params,)
    },
    xcEdit(params) {
        return fetch('/api/external/XCZT_G_Edit', params,)
    },

    /*提货计划*/
    tAdd(params) {
        return fetch('/api/external/TH_G_Add', params,)
    },
    tEdit(params) {
        return fetch('/api/external/TH_G_Edit', params,)
    },
    /*装船出库*/
    zAdd(params) {
        return fetch('/api/external/ZCCK_G_Add', params,)
    },
    zEdit(params) {
        return fetch('/api/external/ZCCK_G_Edit', params,)
    },
    /*装船直装*/
    zcAdd(params) {
        return fetch('/api/external/ZCZZ_G_Add', params,)
    },
    zcEdit(params) {
        return fetch('/api/external/ZCZZ_G_Edit', params,)
    },
    /*进货计划*/
    jAdd(params) {
        return fetch('/api/external/JH_G_Add', params,)
    },
    jEdit(params) {
        return fetch('/api/external/JH_G_Edit', params,)
    },
    receive(params) {
        return fetch('/api/platform/event/receive', params,)
    },
    PlanCancel(params) {
        return fetch('/api/external/PlanCancel', params,)
    },
    //新闻中心 通知公告
    newQuery(params) {
        return fetch('/api/homepage/newQuery', params,)
    },
    getArticleDetail(params) {
        return fetch('/api/content/detail', params, 'get')
    },
    announcementQuery(params) {
        return fetch('/api/homepage/announcementQuery', params, 'post')
    },

    //船期
    vessles(params) {
        return fetch('/api/external/schedules/vessles', params, 'get')
    },
    getSchedulesTerm(params) {
        return fetch('api/external/schedules/term', params, 'get')
    },
    customerByType(params) {
        return fetch('/api/external/schedules/customerByType', params, 'get')
    },
    serviceLines(params) {
        return fetch('/api/external/schedules/serviceLines', params, 'get')
    },
    refCode(params) {
        return fetch('/api/external/schedules/refCode', params, 'get')
    },
    schedulesPage(params) {
        return fetch('/api/external/schedules/page', params, 'get')
    },
    opaAreaCode(params) {
        return fetch('/api/external/schedules/opaAreaCode', params, 'get')
    },
    schedulesOne(params) {
        return fetch('/api/external/schedules/one', params, 'get')
    },
   /* serviceLines(params) {
        return fetch('/api/external/schedules/serviceLines', params, 'get')
    },*/
    find(params) {
        return fetch('/api/external/schedules/find', params, 'get')
    },
    findOne(params) {
        return fetch('/api/external/schedules/findOne', params, 'get')
    },
    addSchedules(params) {
        return fetch('/api/external/schedules/add', params, 'post')
    },
    deleteOrder(params) {
        return fetch('/api/external/planInvalid', params, 'post')
    },
    delSchedules(params) {
        return fetch('/api/external/schedules/del', params, 'get')
    },
    editSchedules(params) {
        return fetch('/api/external/schedules/edit', params, 'post')
    },
    cancelSchedules(params) {
        return fetch('/api/external/schedules/cancel', params, 'get')
    },
    port(params) {
        return fetch('/api/external/schedules/port', params, 'get')
    },
    containerTypeSizeInfo(params) {
        return fetch('/api/external/schedules/containerTypeSizeInfo', params, 'get')
    },
    dangerCode(params) {
        return fetch('/api/external/schedules/dangerCode', params, 'get')
    },
    customer(params) {
        return fetch('/api/external/schedules/customer', params, 'get')
    },
    loadAndUploadSubmit(params) {
        return fetch('/api/external/schedules/loadAndUploadSubmit', params, 'post')
    },
    findOneBeforeBill(params) {
        return fetch('/api/external/schedules/findOneBeforeBill', params, 'get')
    },
    billCheckJX(params) {
        return fetch('/api/external/schedules/billCheckJX', params, 'get')
    },
    billStatusJX(params) {
        return fetch('/api/external/schedules/billStatusJX', params, 'get')
    },
    loadAndUploadFind(params) {
        return fetch('/api/external/schedules/loadAndUploadFind', params, 'get')
    },
    loadAndUploadCheck(params) {
        return fetch('/api/external/schedules/loadAndUploadCheck', params, 'post')
    },
    loadAndUploadFindOne(params) {
        return fetch('/api/external/schedules/loadAndUploadFindOne', params, 'get')
    },
    findSimpleSchedule(params) {
        return fetch('/api/external/schedules/findSimpleSchedule', params, 'get')
    },
    //船名航次新接口
    voyagesVslVoy(params) {
        return fetch('/api/external/voyagesVslVoy', params, 'get')
    },
    //校验接口
    queryIsOkData(params) {
        return fetch('/api/external/schedules/billCheckJX', params, 'get')
    },
    giveAdvice(params) {
        return fetch('/api/external/bMessage/insertSuggest', params, 'post')
    },
    getAdvice(params) {
        return fetch('/api/external/bMessage/queryMessage', params, 'get')
    },
    replyThem(params) {
        return fetch('/api/external/bMessage/insertSuggestReply  ', params, 'post')
    },
    upDateStatus(params) {
        return fetch('/api/external/bMessage/updateRead  ', params, 'put')
    },
	getFleetList(params) {
        return fetch('/api/external/bCar/queryAll', params, 'get')
    },
	insertCarNo(params) {
        return fetch('/api/external/bCar/insertCarNo', params, 'post')
    },
	updateCarNo(params) {
        return fetch('/api/external/bCar/updateCarNo', params, 'put')
    },
	insertDriver(params) {
        return fetch('/api/external/bCar/insertDriver', params, 'post')
    },
	deleteDriver(params) {
        return fetch('/api/external/bCar/deleteDriver', params, 'put')
    },
    containerSelectAll(params) {
        return fetch('/api/external/All_Select', params, 'post')
    },
    generateCode(params) {
        return fetch(`/api/external/bCar/insertCarQRCode?planNo=${params}`, {}, 'post')
    },
    viewCredentials(params) {
        return fetch(`/api/external/bCar/getCarQRCode`, params, 'get')
    },
    editPhone(params) {
        return fetch(`/api/security/updatePhone?phone=${params.phone}`, {}, 'put')
    },
    editemail(params) {
        return fetch(`/api/security/updateEmail?email=${params.email}`, {}, 'put')
    },
    editpassword(params) {
        return fetch(`/api/security/updatePassword`, params, 'put')
    },
    queryCargo(params) {
        return fetch(`/api/external/bCar/getCargo?planNo=${params.planNo}`, {}, 'get')
    },
    queryCustomerUserList(params) {
        return fetch(`/api/external/queryCustomerUserList`, params, 'get')
    },
    addCustomerAccount(params) {
        return fetch(`/api/external/addCustomerAccount`, params, 'post')
    },
    editCustomerAccount(params) {
        return fetch(`/api/external/editCustomerAccount`, params, 'put')
    },
    //集装箱生成二维码
    insertBoxCarQRCode(params) {
        return fetch(`/api/external/bCar/insertBoxCarQRCode?planNo=${params.planNo}`, {},'post')
    },
    //集装箱二维码查看
    getBoxCarQRCode(params) {
        return fetch('/api/external/bCar/getBoxCarQRCode', params,'get')
    },
    //车辆作业查询
	getVehicleList(params) {
        return fetch('/api/external/queryVehicle', params, 'post')
    },
    //货物明细查询
	getGoodDetailsList(params) {
        return fetch('/api/external/queryGoodDetail', params, 'post')
    },
    //计划明细列表
	getProjectList(params) {
        return fetch('/api/external/queryPlanDetail', params, 'post')
    },
    //司机下拉接口查询
    queryDriverList(params) {
        return fetch('/api/external/planTruckCar/listDriver', params,'get')
    },
    //车队计划导入
    importTruckPlan(params) {
        return fetch('/api/external/planTruckCar/importTruckPlan', params, 'post')
    },
    //车队计划新增
    insertTruckPlan(params) {
        return fetch('/api/external/planTruckCar/insert', params, 'post')
    },
    //车队计划删除
   deleteTruckPlan(params) {
        return fetch(`/api/external/planTruckCar/delete?id=${params.id || ''}&truckCompanyCode=${params.truckCompanyCode || ''}`, {}, 'delete')
    },

    //装卸船清单 —— 导出模板
    exportLoadAndUpload(params) {
        return fetch('/api/external/schedules/exportLoadAndUpload', params,'get')
    },
    //装卸船清单 —— 导入
    importLoadAndUpload(params) {
        return fetch('/api/external/schedules/importLoadAndUpload', params, 'post')
    },
    //车队信息 —— 导入
    importLoadAndUploadCar(params) {
        return fetch('/api/external/bCar/importExcel', params, 'post')
    },
    //车辆计划模板下载
    getTemplate(params) {
        return fetch('/api/external/planTruckCar/exportLoadAndUpload', params, 'get')
    },

    //集装箱车辆作业运输明细查询
    containerVehiclePage(params) {
        return fetch('/api/external/container/vehicle-details/page', params, 'post')
    },

     // 车辆运输明细导出
     vehicleDetailExport(params) {
        return fetch('/api/external/vehicle-detail/export', params,'post')
    }

}
